
#include "WorldObject.h"
#include "World.h"
#include <cassert>

static unsigned current_world_uid = 0;

WorldObject::WorldObject() : world_uid(current_world_uid) { 
    current_world_uid++; 
}

WorldObject::~WorldObject(){
}

unsigned WorldObject::getId(void) const { 
    return world_uid; 
}



WorldObjectPointer::WorldObjectPointer(){
    bound = false;
    p = NULL;
    id = 0;
}

WorldObjectPointer::WorldObjectPointer(WorldObject *p) : p(p), id(p->getId()), bound(true) {
    assert(p != NULL);
}

WorldObjectPointer::WorldObjectPointer(const WorldObjectPointer &obj){
    p = obj.p;
    id = obj.id;
    bound = obj.bound;
}

WorldObjectPointer& WorldObjectPointer::operator= (const WorldObjectPointer &obj){
    p = obj.p;
    id = obj.id;
    bound = true;
    return *this;
}

WorldObjectPointer::~WorldObjectPointer(){
}

bool WorldObjectPointer::isValid(void) const {
    if(!bound){ return false; }
    
    WorldObject *obj = World::instance().getObject(id);
    return obj != NULL && obj->active();
}

WorldObject* WorldObjectPointer::getPointer(void) const {
    if(!bound){ return NULL; }
    return World::instance().getObject(id);
}

